/**=========================================================
 * Service: simple-select.js
 =========================================================*/

App.service('SimpleSelect', ['Utils', 'HttpUtils',
    function(utils, httpUtils) {
        'use strict';
        
        return {
            init: function($scope, options) {
                var defaultOptions = {
                    /**
                     * $scope.selects对象中的属性名
                     */
                    optionModel: '',
                    /**
                     * 选中值实际绑定的变量名
                     * 如果绑定了自定义的变量 并且需要触发值变更事件时 需要指定
                     */
                    ngModel: '',
                    /**
                     * 后台的查询参数
                     */
                    params: '',
                    /**
                     * 是否请求后台
                     */
                    remote: true,
                    /**
                     * 是否实时刷新
                     */
                    refresh: false,
                    /**
                     * 本地数据
                     * {name: 'name', value: 'value'}
                     */
                    datas: [],
                    /**
                     * 是否多选
                     */
                    multiple: false,
                    /**
                     * 初始值
                     */
                    initValue: '',
                    /**
                     * 初始名称
                     */
                    initName: '',
                    /**
                     * 初始过滤条件
                     * {attr:'filter',value:'-9999'}
                     */
                    initFilter: null,
                    /**
                     * 值变更事件
                     */
                    selectChangeEvent: null,
                    /**
                     * 是否自动加载
                     */
                    autoLoad: true,
                    /**
                     * 是否默认选择第一个
                     */
                    autoSelect: false
                }
                
                var finalOptions = angular.extend(defaultOptions,options);
                if (finalOptions.multiple && utils.isEmpty(finalOptions.initValue)){
                    finalOptions.initValue = [];
                }
                
                if (utils.isEmpty($scope.selects)) {
                    $scope.selects = {};
                }
                
                $scope.selects[finalOptions.optionModel] = {};
                
                if (finalOptions.refresh){
                    $scope.selects[finalOptions.optionModel].selected = finalOptions.initValue;
                    $scope[finalOptions.optionModel + "Query"] = function(inputValue){
                        finalOptions.params.inputValue = inputValue;
                        if (!utils.isEmpty(inputValue)){
                            httpUtils.queryList(finalOptions.params,
                                function(result){
                                    var allDatas = result.datas.gridDatas;
                                    
                                    for (var i = 0; i < allDatas.length; i++){
                                        allDatas[i].value = allDatas[i].value + "";
                                    }
                                    
                                    $scope.selects[finalOptions.optionModel].allDatas = allDatas;
                                    
                                    if (utils.isEmpty(finalOptions.initFilter)){
                                        $scope.selects[finalOptions.optionModel].datas = allDatas;
                                    }else{
                                        var datas = [];
                                        for (var i = 0; i < allDatas.length; i++){
                                            if (allDatas[i][finalOptions.initFilter.attr] == finalOptions.initFilter.value){
                                                datas.push(utils.clone(allDatas[i]));
                                            }
                                        }
                                        
                                        $scope.selects[finalOptions.optionModel].datas = datas;
                                    }
                                }
                            );
                        }else{
                            $scope.selects[finalOptions.optionModel].allDatas = [];
                            $scope.selects[finalOptions.optionModel].datas = [];
                        }
                    }
                }else if (finalOptions.autoLoad) {
                    //获取数据
                    if (finalOptions.remote){
                        httpUtils.queryList(finalOptions.params,
                            function(result){
                                var allDatas = result.datas.gridDatas;
                                
                                for (var i = 0; i < allDatas.length; i++){
                                    allDatas[i].value = allDatas[i].value + "";
                                }
                                
                                $scope.selects[finalOptions.optionModel].allDatas = allDatas;
                                
                                if (utils.isEmpty(finalOptions.initFilter)){
                                    $scope.selects[finalOptions.optionModel].datas = allDatas;
                                }else{
                                    var datas = [];
                                    for (var i = 0; i < allDatas.length; i++){
                                        if (allDatas[i][finalOptions.initFilter.attr] == finalOptions.initFilter.value){
                                            datas.push(utils.clone(allDatas[i]));
                                        }
                                    }
                                    
                                    $scope.selects[finalOptions.optionModel].datas = datas;
                                }

                                if (finalOptions.autoSelect){
                                    $scope.selects[finalOptions.optionModel].selected =  $scope.selects[finalOptions.optionModel].datas[0].value;
                                }   
                            }
                        );
                    }else{
                        var allDatas = finalOptions.datas;
                                
                        $scope.selects[finalOptions.optionModel].allDatas = allDatas;
                        
                        if (utils.isEmpty(finalOptions.initFilter)){
                            $scope.selects[finalOptions.optionModel].datas = allDatas;
                        }else{
                            var datas = [];
                            for (var i = 0; i < allDatas.length; i++){
                                if (allDatas[i][finalOptions.initFilter.attr] == finalOptions.initFilter.value){
                                    datas.push(utils.clone(allDatas[i]));
                                }
                            }
                            
                            $scope.selects[finalOptions.optionModel].datas = datas;
                        }
                    }
                    
                    //初始值
                    if (!utils.isEmpty(finalOptions.initValue)){
                        $scope.selects[finalOptions.optionModel].selected = finalOptions.initValue;
                    }
                    
                    
                    //值变更事件
                    if (finalOptions.selectChangeEvent instanceof Function){
                        var variable = utils.isEmpty(finalOptions.ngModel) ? 'selects.' + finalOptions.optionModel + '.selected' : finalOptions.ngModel;
                        $scope.$watch(variable,
                            function (newVal, oldVal) {                       
                                finalOptions.selectChangeEvent.call(this,newVal,oldVal);
                            }
                        );
                    }
                }
                
                return {
                    scope: $scope,
                    options: finalOptions,
                    /**
                     * 根据值获取对应的对象
                     */
                    getData:function(value){
                        var scope = this.scope,
                            options = this.options,
                            datas = scope.selects[options.optionModel].datas;
                        if (utils.isEmpty(datas)){
                            return {value: options.initValue, name: options.initName};
                        }else if (utils.isEmpty(value)){
                            return value;
                        }else{
                            for (var i = 0; i < datas.length; i++){
                                if (datas[i].value == value){
                                    return datas[i];
                                }
                            }
                        }
                    },
                    /**
                     * 获取所有的值
                     */
                    getAllData:function(value){
                        var scope = this.scope,
                            options = this.options,
                            datas = scope.selects[options.optionModel].datas;
                        return datas;
                    },
                    /**
                     * 获取选中值
                     */
                    getSelected: function(){
                        return utils.isEmpty(this.scope.selects[this.options.optionModel].selected) ? '' : this.scope.selects[this.options.optionModel].selected;
                    },
                    /**
                     * 获取选择的名称
                     */
                    getSelectedName: function(){
                        return this.getData(this.getSelected()).name;
                    },
                    /**
                     * 选中指定值
                     * 如果refresh=true 则传入的值为[{name=name,value=value}]
                     */
                    select: function(newVal){
                        var scope = this.scope,
                            options = this.options
                        if (options.refresh){
                            scope.selects[options.optionModel].datas = newVal;
                            if (utils.isEmpty(options.ngModel)){
                                scope.selects[options.optionModel].selected = newVal[0].value;
                            }else{
                                eval("this.scope." + options.ngModel + "=" + newVal[0].value);
                            }
                        }else{
                            if (utils.isEmpty(options.ngModel)){
                                scope.selects[options.optionModel].selected = newVal;
                            }else{
                                eval("this.scope." + options.ngModel + "=" + newVal);
                            }
                        }
                    },
                    /**
                     * 选中第一个值
                     */
                    selectFirst: function(newVal){
                        var scope = this.scope,
                            options = this.options,
                            datas = scope.selects[options.optionModel].datas;
                        if (!utils.isEmpty(datas) && datas.length > 0){
                            this.select(datas[0].value)
                        }
                    },
                    /**
                     * 刷新数据
                     */
                    refresh: function(params){
                        var scope = this.scope,
                            options = this.options
                            options.params = params;

                        httpUtils.queryList(params,
                            function(result){
                                var allDatas = result.datas.gridDatas;
                                
                                for (var i = 0; i < allDatas.length; i++){
                                    allDatas[i].value = allDatas[i].value + "";
                                }
                                
                                scope.selects[options.optionModel].allDatas = allDatas;
                                
                                if (utils.isEmpty(options.initFilter)){
                                    scope.selects[options.optionModel].datas = allDatas;
                                }else{
                                    var datas = [];
                                    for (var i = 0; i < allDatas.length; i++){
                                        if (allDatas[i][options.initFilter.attr] == options.initFilter.value){
                                            datas.push(utils.clone(allDatas[i]));
                                        }
                                    }
                                    
                                    scope.selects[options.optionModel].datas = datas;
                                }
                            }
                        );
                    },
                    /**
                     * 清空选择
                     */
                    clear: function(){
                        if (this.options.multiple){
                            this.select([]);
                        }else{
                            this.select('');
                        }
                    },
                    /**
                     * 过滤结果集
                     */
                    filter: function(attr,value,logic){
                        var scope = this.scope,
                            options = this.options,
                            datas = [],
                            allDatas = scope.selects[options.optionModel].allDatas;
                        
                        if (utils.isEmpty(allDatas)) return;
                        
                        if (logic == "!="){
                            if (value instanceof Object){
                                for (var i = 0; i < allDatas.length; i++){
                                    var needFilter = false
                                    for (var j = 0; j < value.length; j++){
                                        if (allDatas[i][attr] == value[j]){
                                            needFilter = true;
                                            break;
                                        }
                                    }
                                    if (!needFilter){
                                        datas.push(utils.clone(allDatas[i]));
                                    }
                                }
                            }else{                      
                                for (var i = 0; i < allDatas.length; i++){
                                    if (allDatas[i][attr] != value){
                                        datas.push(utils.clone(allDatas[i]));
                                    }
                                }
                            }
                        }else{
                            if (value instanceof Object){
                                for (var i = 0; i < allDatas.length; i++){
                                    for (var j = 0; j < value.length; j++){
                                        if (allDatas[i][attr] == value[j]){
                                            datas.push(utils.clone(allDatas[i]));
                                        }
                                    }   
                                }
                            }else{                      
                                for (var i = 0; i < allDatas.length; i++){
                                    if (allDatas[i][attr] == value){
                                        datas.push(utils.clone(allDatas[i]));
                                    }
                                }
                            }
                        }
                        
                        scope.selects[options.optionModel].datas = datas;
                    },
                    /**
                     * 取消过滤
                     */
                    clearFilter: function(){
                        var scope = this.scope,
                            options = this.options,
                            datas = [],
                            allDatas = scope.selects[options.optionModel].allDatas;
                            
                        if (utils.isEmpty(allDatas)) return;
                        
                        for (var i = 0; i < allDatas.length; i++){
                            datas.push(utils.clone(allDatas[i]));
                        }
                        
                        scope.selects[options.optionModel].datas = datas;
                    }
                };
            }
        }
    }
]);
